//
//  InputAccessoryBar.m
//  WeKissed
//
//  Created by luo danal on 8/7/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "InputAccessoryBar.h"

@implementation InputAccessoryBar
@synthesize inputFields = _inputFields;
@synthesize rootController = _rootController;

- (void)dealloc{
    Block_release(_doneBlock);
    [_inputFields release];
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    frame.size.height = 44.f;
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.barStyle = UIBarStyleBlackTranslucent;
        UIBarButtonItem *prevbbt = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Prev", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(prev:)];
        UIBarButtonItem *nextbbt = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Next", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(next:)];
        UIBarButtonItem *donebbt = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(done:)];
        UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
        self.items = [NSArray arrayWithObjects:prevbbt, nextbbt, flex, donebbt, nil];
        [prevbbt release];
        [nextbbt release];
        [donebbt release];
        [flex release];
        
        self.inputFields = [NSArray array];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)setDoneBlock:(void (^)(id))block{
    Block_release(_doneBlock);
    _doneBlock = Block_copy(block);
}
                      
- (void)addField:(UIControl *)aField tag:(NSInteger)tag{
    aField.tag = tag;
    NSMutableArray *fields = [NSMutableArray arrayWithArray:self.inputFields];
    int i = 0;
    for (UIControl *ctrl in fields) {
        if (ctrl.tag == tag) {
            [fields replaceObjectAtIndex:i withObject:aField];
            self.inputFields = fields;
            return;
        }
        i++;
    }
    [fields addObject:aField];
    self.inputFields = fields;
}

- (void)removeFieldWithTag:(NSInteger)tag{
    NSMutableArray *fields = [NSMutableArray arrayWithArray:self.inputFields];
    for (UIControl *ctrl in fields) {
        if (ctrl.tag == tag) {
            [fields removeObject:ctrl];
            self.inputFields = fields;
            break;
        }
    }

}

- (void)removeAllFields{
    self.inputFields = [NSArray array];
}

- (IBAction)prev:(id)sender{
    
    UIControl *lastCtrl = nil;
    int i = 0;
    for (UIControl *ctrl in self.inputFields){
        if (ctrl.isFirstResponder) {
            [ctrl resignFirstResponder];
            [lastCtrl becomeFirstResponder];
            break;
        }
        lastCtrl = ctrl;
        i++;
    }
}

- (IBAction)next:(id)sender{
    UIControl *lastCtrl = nil;
    int i = 0;
    for (UIControl *ctrl in self.inputFields){
        if (lastCtrl.isFirstResponder) {
            [lastCtrl resignFirstResponder];
            [ctrl becomeFirstResponder];
            break;
        }
        lastCtrl = ctrl;
        i++;
    }
}

- (IBAction)done:(id)sender{
    for (UIControl *ctrl in self.inputFields){
        if (ctrl.isFirstResponder) {
            [ctrl resignFirstResponder];
        }
    }
    if (_doneBlock != NULL) {
        _doneBlock(self.rootController);
    }
}

@end
